package com.aruio.controller;

import com.arui.Result;
import com.aruio.feign.PandaClient;
import com.aruio.service.TestService;
import com.aruio.thread.ThreadPoolExecutorMdcWrapper;
import com.aruio.vo.TestVO;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.apache.tomcat.jni.User;
import org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpSession;

/**
 * @Description 测试控制类
 * @Date 2024/9/11 10:51
 * @Created by 10574
 */
@RestController
@RequestMapping("test")
@RequiredArgsConstructor
@Slf4j
public class TestController {


    private final TestService testService;
    private static final ThreadPoolTaskExecutor executor;
    private final PandaClient pandaClient;

    static {
        executor = new ThreadPoolExecutorMdcWrapper();
        executor.initialize();
    }

    @RequestMapping("session")
    public String sessionTest(HttpSession session) {
        final User user = new User();
        session.setAttribute("user", user);
        return "";
    }

    @GetMapping("hello")
    public Result<Object> testHello() {
        log.info("--------开始Hello--------");
        try {
            Thread.sleep(1000);
            log.info("--------hello--------");
            for (int i = 0; i < 10; i++) {
                executor.submit(() -> {
                    log.info("test123");
                });
            }
        } catch (InterruptedException e) {
            throw new RuntimeException(e);
        }
        log.info("--------Hello完毕--------");
        return Result.status(true);
    }

    @GetMapping("async")
    public Result<String> testAsync() {
        log.info("--------start async--------");
        testService.doSomething();
        log.info("--------finish async--------");
        return Result.status(true);
    }

    @GetMapping("feign")
    public Result<TestVO> testFeign() {
        return pandaClient.test();
    }
}
